package org.example.petitionplatformsystem.controller;
import org.example.petitionplatformsystem.Response;
import org.example.petitionplatformsystem.dao.model.Institution;
import org.example.petitionplatformsystem.service.InstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin_panel")
public class InstitutionController {
    //机构表的服务
    private final InstitutionService institutionService;
    @Autowired
    public InstitutionController(InstitutionService institutionService){
        this.institutionService = institutionService;
    }

    @PostMapping("/institution")
    public Response<Void> addInstitution(@RequestParam String institutionName, @RequestParam(required = false) String description) {
        Institution institution = new Institution();
        institution.setInstitutionName(institutionName);
        institution.setDescription(description);
        Institution createdInstitution = institutionService.AddInstitution(institution);
        return createdInstitution != null ? Response.newSuccessResponse(null) : Response.newFailResponse("Failed to create institution");
    }

    @DeleteMapping("/institution/{institutionId}")
    public Response<Void> deleteInstitution(@PathVariable Long institutionId) {
        Institution institution = institutionService.getInstitutionById(institutionId);
        if (institution == null) {
            return Response.newFailResponse("Institution not found");
        }
        institutionService.DeleteInstitution(institutionId);
        return Response.newSuccessResponse(null);
    }

    @PutMapping("/institution/{institutionId}")
    public Response<Void> updateInstitution(@PathVariable Long institutionId, @RequestParam String institutionName, @RequestParam(required = false) String description) {
        Institution existingInstitution = institutionService.getInstitutionById(institutionId);
        if (existingInstitution == null) {
            return Response.newFailResponse("Institution not found");
        }
        existingInstitution.setInstitutionName(institutionName);
        existingInstitution.setDescription(description);
        Institution updatedInstitution = institutionService.UpdateInstitution(existingInstitution);
        return updatedInstitution != null ? Response.newSuccessResponse(null) : Response.newFailResponse("Failed to update institution");
    }

    @GetMapping("/institutions")
    public Response<List<Institution>> getAllInstitutions() {
        List<Institution> institutions = institutionService.GetAllInstitution();
        return Response.newSuccessResponse(institutions);
    }

    @GetMapping("/institution/{institutionId}")
    public Response<Institution> getInstitutionById(@PathVariable Long institutionId) {
        Institution institution = institutionService.getInstitutionById(institutionId);
        return institution != null ? Response.newSuccessResponse(institution) : Response.newFailResponse("Institution not found");
    }
}
